package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EnterpriseCommonParam
import cn.exrick.xboot.base.mapper.EnterpriseCommonParamMapper
import cn.exrick.xboot.base.service.IEnterpriseCommonParamService
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.hutool.core.date.DateUnit
import cn.hutool.core.date.DateUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 企业通用参数接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IEnterpriseCommonParamServiceImpl : ServiceImpl<EnterpriseCommonParamMapper?, EnterpriseCommonParam?>(), IEnterpriseCommonParamService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService


    override fun createOrUpdateEnterpriseCommonParam(enterpriseCommonParam: EnterpriseCommonParam): Result<Any?>? {

        return try {

            enterpriseCommonParam.enterpriseUserId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id)
            enterpriseCommonParam.examStartTime = DateUtil.parse(enterpriseCommonParam.examStartTimeStr, "yyyy-MM-dd")
            saveOrUpdate(enterpriseCommonParam)
            ResultUtil.success("操作成功")
        }catch (e : Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeEnterpriseCommonParam(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("操作成功")
        }catch (e : Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getEnterpriseCommonParamList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(EnterpriseCommonParam::class.java)
                .eq(EnterpriseCommonParam::enterpriseUserId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(EnterpriseCommonParam::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, this)

            ResultUtil.data(result, "获取数据成功")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun getEnterpriseCommonParamLast(): EnterpriseCommonParam? {

        return getOne(KtQueryWrapper(EnterpriseCommonParam::class.java).eq(EnterpriseCommonParam::enterpriseUserId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(EnterpriseCommonParam::createTime).last("limit 1"))
    }


}